using Config;
using Data;
using Entity.Monster.State;
using Map;
using MoonFramework.Scripts.Tool;
using MoonFramework.Scripts.Tool.System.Setting;
using MoonFramework.Scripts.View.Music;
using Tool.Name;
using UnityEngine;
using UnityEngine.AI;

namespace Entity.Monster
{
    public abstract class MonsterObject : MonoBehaviour
    {
        #region 组件
        [SerializeField] protected NavMeshAgent _navMeshAgent;
        public NavMeshAgent NavMeshAgent => _navMeshAgent;
        public Collider InputCheckCollider => GetComponent<Collider>();
        public Monster Monster;
        #endregion
        
        #region 存档数据
        protected MapObjectData aiData;
        public MapObjectData AIData => aiData;
        #endregion
        
        #region 属性
        protected float hp;
        protected MapVertexType _mapVertexType;
        protected BaseMapBlock _mapBlock; //当前所在的地图块
        [SerializeField] private int lootConfigID = -1;
        #endregion
        
        public MapVertexType MapVertexType => _mapVertexType;
        public BaseMapBlock MapBlock => _mapBlock;
        
        public void OnInit(Monster monster) => Monster = monster;

        public virtual void Init(BaseMapBlock mapBlock, MapObjectData mapObjectData)
        {
            hp = Monster.MonsterConfig.MaxHp;
            _mapBlock = mapBlock;
            aiData = mapObjectData;
            transform.position = aiData.position;
        }

        public void InitOnTransfer(BaseMapBlock mapBlock)
        {
            _mapBlock = mapBlock;
        }

        #region 基本行为

        private Character player;
        public bool DoHit(Actor actor, float dir)
        {
            if (dir > Monster.MonsterConfig.Radius) return false;
            //空手没有攻击力，默认空手不能对Monster造成伤害
            // player = (Character)actor;
            return (actor as Character)?.WeaponController.CurrentWeapon != null;
        }

        public void Hurt(ItemWeaponInfo weaponInfo)
        {
            if(hp <= 0)return;
            AudioManager.Instance.PlayOnShot(weaponInfo.AttackAudio, transform.position).Forget();
            // AudioManager.Instance.PlayOnShot((player.WeaponController.CurrentWeapon.WeaponData?.Config?.ItemTypeInfo as ItemWeaponInfo)?.HitAudio, 
            //     transform.position).Forget();
            Monster.fsmMachine.ChangeState(nameof(MonsterHurtState)); //转换到受击状态
            float attackValue = weaponInfo.AttackValue;
            hp -= attackValue;
            if (hp <= 0)
            {
                //切换到死亡逻辑
                Monster.fsmMachine.ChangeState(nameof(MonsterDeadState));    
            }
        }
        
        /// <summary>
        /// 只是逻辑层死亡
        /// </summary>
        public void Destroy()
        {
            Monster.fsmMachine.Stop();
            Monster.buffFsm.Stop();
            gameObject.MoonGameObjPushPool();
        }

        /// <summary>
        /// 彻底的死亡
        /// 数据层面，游戏物体都要销毁
        /// </summary>
        public void Dead()
        {
            // 告知地图块移除自己
            _mapBlock.RemoveAIObject(aiData.ID);
            LootObject();
            Debug.Log(1);
        }
        
        private void LootObject()
        {
            if(lootConfigID == -1) return; //没有掉落物
            Debug.Log(2);
            LootConfig lootConfig = ConfigManager.Instance.GetConfig<LootConfig>(ConfigName.Loot, lootConfigID);
            if(!lootConfig) return;
            lootConfig.GetMapObject(_mapBlock, transform.position + Vector3.up);
        }
        #endregion
        
        /// <summary>
        /// 获取AI可以到达的随机坐标
        /// </summary>
        public Vector3 GetAIRandomPoint(MapVertexType mapVertexType)
        {
            return _mapBlock.GetAIRandomPoint(mapVertexType);
        }

        /// <summary>
        /// 保存坐标
        /// </summary>
        public void SavePos()
        {
            aiData.position = transform.position;
        }
    }
}